/**
 * @author liu_j@worksap.co.jp
 */
Ext.define('Goban.controller.ControlPanel', {
	extend: 'Ext.app.Controller',
	requires: ['Goban.data.Goban'],
	views: ['ControlPanel', 'AnalysisGrid'],
	stores: ['Player'],
	init: function() {
		this.control({
			'controlPanel > button#start': {
				click: this.onStartButtonClick
			},
			'controlPanel > combo#blackPlayerCombo': {
				change: this.onBlackComboChange
			},
			'controlPanel > combo#whitePlayerCombo': {
				change: this.onWhiteComboChange
			},
			'controlPanel': {
				countTime: this.onCountTime,
				record: this.onRecordResults
			}
		});
	},
	onStartButtonClick: function(button, e, eOpts) {
		var gobanPanel = button.up('viewport').down('gobanPanel');
		var controlPanel = button.up('controlPanel');
		Goban.data.Goban.BLACKPLAYER = controlPanel.down('combo#blackPlayerCombo').getValue();
		Goban.data.Goban.WHITEPLAYER = controlPanel.down('combo#whitePlayerCombo').getValue();
		Goban.data.Goban.BLACKLEVEL = controlPanel.down('numberfield#blackLevelField').getValue();
		Goban.data.Goban.WHITELEVEL = controlPanel.down('numberfield#whiteLevelField').getValue();
		gobanPanel.fireEvent('initGoban', gobanPanel);
		var midX = Math.floor(Goban.data.Goban.XNUM/2);
		var midY = Math.floor(Goban.data.Goban.YNUM/2);
		gobanPanel.fireEvent('putGo', gobanPanel, midX, midY);
		this.lastTime = (new Date()).getTime();
		this.blackTotalTime = 0;
		this.whiteTotalTime = 0;
		this.blackTotalStep = 0;
		this.whiteTotalStep = 0;
		controlPanel.down('displayfield#blackAverageTimeField').setValue(0);
		controlPanel.down('displayfield#whiteAverageTimeField').setValue(0);
		controlPanel.down('analysisGird#blackAnalysisGrid').getStore().removeAll(false);
		controlPanel.down('analysisGird#whiteAnalysisGrid').getStore().removeAll(false);
	},
	onBlackComboChange: function(combo, newValue, oldValue, eOpts) {
		var blackLevelField = combo.up('controlPanel').down('numberfield#blackLevelField');
		if(newValue===0) {
			blackLevelField.hide();
		} else {
			blackLevelField.show();
		}
	},
	onWhiteComboChange: function(combo, newValue, oldValue, eOpts) {
		var whiteLevelField = combo.up('controlPanel').down('numberfield#whiteLevelField');
		if(newValue===0) {
			whiteLevelField.hide();
		} else {
			whiteLevelField.show();
		}
	},
	onCountTime: function(panel, goType) {
		if(!this.lastTime) {
			return;
		}
		var nowTime = (new Date()).getTime();
		var usedTime = nowTime - this.lastTime;
		this.lastTime = nowTime;
		if(goType===-1) {
			this.blackTotalTime += usedTime;
			this.blackTotalStep++;
			panel.down('displayfield#blackAverageTimeField').setValue(this.blackTotalTime/this.blackTotalStep);
		} else {
			this.whiteTotalTime += usedTime;
			this.whiteTotalStep++;
			panel.down('displayfield#whiteAverageTimeField').setValue(this.whiteTotalTime/this.whiteTotalStep);
		}
	},
	onRecordResults: function(panel, goType, nodeNum, usedTime) {
		var store;
		if(goType<0) {
			store = panel.down('analysisGird#blackAnalysisGrid').getStore();
		} else {
			store = panel.down('analysisGird#whiteAnalysisGrid').getStore();
		}
		store.add({nodeNum: nodeNum, time: usedTime});
	}
});